#!/bin/bash
#检测x86网络连接，根据实际情况修改ping地址，可以改为www.baidu.com等
function x86_detect_connectivity() {
  if ping -c 1 114.114.114.114 >/dev/null 2>&1; then
    echo 1
  else
    echo 0
  fi
}
#检查x86软链接路径是否正常即 /apollo -> /data/.davinci_data_collector/out
function x86_detect_soft_link_2apollo() {
  softPath=$(realpath /apollo)
  if [ "$softPath" == "/data/.davinci_data_collector/out" ]; then
    echo 1
  else
    echo 0
  fi
}
#检查x86上的ttyUSB的软链是否链上
function X86_detect_ttyUSB_mount() {
  ttyList=$(ls -la /dev | grep 'ttyUSB*' | awk '{print $9}')
  num=3
  for i in $ttyList; do
    if [ "$i" == "ttyUSB_IMU_APOLLO" ]; then #判断字符串是否相等，注意前后要有空格，否则变为赋值语句
      num=$((num-1))
    elif [ "$i" == "ttyUSB_RTK_APOLLO" ]; then #判断字符串是否相等，注意前后要有空格，否则变为赋值语句
      num=$((num-1))
    elif [ "$i" == "ttyUSB_RTK_ROS" ]; then #判断字符串是否相等，注意前后要有空格，否则变为赋值语句
      num=$((num-1))
    fi
  done
  if [ $num == 0 ];then
    echo 1
  else
    echo 0
  fi
}
#检测x86连接nas情况
function x86_detect_connect_2nas() {
  if ping -c 1 10.10.10.100 >/dev/null 2>&1; then
    echo 1
  else
    echo 0
  fi
}
#检测nas是否开启5005端口(参照:https://blog.csdn.net/R_W_zhang/article/details/103494257)
function nas_detect_open_5005() {
  if [ "$2" == "x86" ];then
    HOST=10.10.10.100
  else
    HOST=192.168.8.66
  fi
  PORT=5005
  if (echo >/dev/tcp/${HOST}/${PORT}) &>/dev/null; then
    echo 1
  else
    echo 0
  fi
}
#检测x86连接8155情况
function x86_detect_connect_2_8155() {
  if adb root >/dev/null 2>&1; then
    echo 1
  else
    echo 0
  fi
}
#检测8155连接nas情况
function 8155_detect_connect_2nas() {
  if [ "$2" == "x86" ];then
    adb root >/dev/null 2>&1;
    if adb shell ping -c 1 192.168.8.66 >/dev/null 2>&1; then
      echo 1
    else
      echo 0
    fi
  else
    if ping -c 1 192.168.8.66 >/dev/null 2>&1; then
      echo 1
    else
      echo 0
    fi
  fi
}
#检测nas是否挂载成功
function nas_detect_mount_2x86(){
  if [ -d "/media/didi/nas_didi/didi" ]; then
    echo 1
  else
    echo 0
  fi
}

if [ "$1" == "x86_conn_internet" ];then
  x86_detect_connectivity
elif [ "$1" == "x86_apollo_link" ]; then
  x86_detect_soft_link_2apollo
elif [ "$1" == "x86_ttyUSB_link" ]; then
  X86_detect_ttyUSB_mount
elif [ "$1" == "x86_conn_nas" ]; then
  x86_detect_connect_2nas
elif [ "$1" == "nas_5005" ]; then
  nas_detect_open_5005 "$2"
elif [ "$1" == "x86_conn_8155" ]; then
  x86_detect_connect_2_8155
elif [ "$1" == "x86_mount_nas" ]; then
  nas_detect_mount_2x86
elif [ "$1" == "8155_conn_nas" ]; then
  8155_detect_connect_2nas "$2"
fi
